package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ScrollView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.TabBasicActivity;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.HomeEntity;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mytools.tool.utils.StringUtil;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends TabBasicActivity {

	private PullToRefreshScrollView scroolView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_main);
		super.onCreate(savedInstanceState);
		initView();
		
		//百度定位
		//mApp.locationClient.start();
		
		findViewById(R.id.layout_point_activate).setOnClickListener(this);
		findViewById(R.id.layout_coupon_activate).setOnClickListener(this);
		findViewById(R.id.layout_coupon).setOnClickListener(this);
		findViewById(R.id.layout_consumption).setOnClickListener(this);
		findViewById(R.id.layout_transaction).setOnClickListener(this);
		findViewById(R.id.layout_point_verification).setOnClickListener(this);
		findViewById(R.id.layout_coupon_verification).setOnClickListener(this);
		
		//网络请求
		getdata();
		
	}
	
	@SuppressWarnings("deprecation")
	private void initView(){
		showView(R.id.titleback_btn_back, false);
		setTvText(R.id.tv_title, "首页");
		scroolView = (PullToRefreshScrollView) findViewById(R.id.scroolView);
		scroolView.setMode(PullToRefreshBase.Mode.PULL_DOWN_TO_REFRESH);
		scroolView.setOnRefreshListener(new PullToRefreshViewEvent()); //下拉刷新 上拉加载
	}
	
	private void getdata(){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		API.http_post(this, API.HOMEPAGE, params, new HttpCallback<HomeEntity>(this) {
			@Override
			public void onSuccess(int statusCode, HomeEntity data) {
				scroolView.onRefreshComplete();
				setTvText(R.id.tv_shopMemberYesterday, data.getShopMemberYesterday()+"人");
				setTvText(R.id.tv_shopMemberToday, data.getShopMemberToday()+"人");
				setTvText(R.id.tv_turnoverYesterday, StringUtil.getMoneyString(data.getTurnoverYesterday())+"元");
				setTvText(R.id.tv_turnoverToday, StringUtil.getMoneyString(data.getTurnoverToday())+"元");
				setTvText(R.id.tv_pointIssueYesterday, data.getPointIssueYesterday()+"点");
				setTvText(R.id.tv_pointVerificatYesterday, data.getPointVerificatYesterday()+"点");
				setTvText(R.id.tv_couponIssueYesterday, data.getCouponIssueYesterday()+"张");
				setTvText(R.id.tv_couponVerificatYesterday, data.getCouponVerificatYesterday()+"张");
				//scan();
			}

			@Override
			public void onFailure(int statusCode, String message) {
				scroolView.onRefreshComplete();
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<HomeEntity>() {
				};
			}
		});
	}
	
	//下拉刷新 上拉加载
	public class PullToRefreshViewEvent implements PullToRefreshBase.OnRefreshListener<ScrollView> {
		@Override
		public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
			getdata();
		}
	}
	
//	@Override
//	public void onReceiveLocation(BDLocation arg0) {
//		Toast.makeText(MainActivity.this, arg0.getCity(), Toast.LENGTH_SHORT).show();
//		super.onReceiveLocation(arg0);
//	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.layout_point_activate:
				startActivity(new Intent(mActivity, PointActivateActivity.class));
				break;
			case R.id.layout_coupon_activate:
				startActivity(new Intent(mActivity, CouponActivateActivity.class));
				break;
			case R.id.layout_coupon:
				startActivity(new Intent(mActivity, CouponActivity.class));
				break;
			case R.id.layout_consumption:
				startActivity(new Intent(mActivity, ConsumptionActivity.class));
				break;
			case R.id.layout_transaction:
				startActivity(new Intent(mActivity, TransactionActivity.class));
				break;
			case R.id.layout_point_verification:
				startActivity(new Intent(mActivity, PointVerificationActivity.class));
				break;
			case R.id.layout_coupon_verification:
				startActivity(new Intent(mActivity, CouponVerificationActivity.class));
				break;
			default:break;
		}
	}
	
}
